package com.ssmk.module.shoppingcar.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.LogUtils;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.ssmk.R;
import com.ssmk.module.shoppingcar.bean.OrderAllBean;
import com.ssmk.module.shoppingcar.bean.SaleAfterBean;
import com.ssmk.module.shoppingcar.bean.SaleAfterSkuInfoBean;
import com.ssmk.module.shoppingcar.bean.SkuInfoBean;
import com.ssmk.module.shoppingcar.order.LogisticsDetailsActivity;

import java.util.List;

/**
 * Created by Administrator on 2017/9/5 0005.
 */

public class SaleAfterAdapter extends RecyclerView.Adapter<SaleAfterAdapter.SaleAfterHolder> implements View.OnClickListener {
    private final List<SaleAfterBean.DataBean.RowsBean> list;
    private Context mContext;
    private int mStatus;

    public SaleAfterAdapter(Context context, List<SaleAfterBean.DataBean.RowsBean> datas) {
        this.mContext = context;
        this.list = datas;
    }

    @Override
    public SaleAfterHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.aftersell_recycler_item,parent,false);
        SaleAfterHolder holder = new SaleAfterHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(SaleAfterHolder holder, int position) {
        String skuInfo = list.get(position).getSkuInfo();
        Gson gson = new Gson();
        SaleAfterSkuInfoBean skuInfoBean = gson.fromJson(skuInfo, SaleAfterSkuInfoBean.class);
        if (!TextUtils.isEmpty(skuInfoBean.getCommodityFormat().getGoodsFormatPhoto())){
            Glide.with(mContext).load(skuInfoBean.getCommodityFormat().getGoodsFormatPhoto()).into(holder.allorder_shop_iv);
        }else {
            Toast.makeText(mContext,"图片加载失败",Toast.LENGTH_SHORT).show();
        }
        if (!TextUtils.isEmpty(list.get(position).getGoodsName())){
            holder.allorder_shop_name.setText(list.get(position).getGoodsName());
        }else {
            Toast.makeText(mContext,"",Toast.LENGTH_SHORT).show();
        }
        if (!TextUtils.isEmpty(list.get(position).getPurchaseSize()+"")){
            holder.allorder_shop_count.setText(list.get(position).getPurchaseSize()+"");
        }else {
            Toast.makeText(mContext,"",Toast.LENGTH_SHORT).show();
        }
        if (skuInfoBean != null){
            if (!TextUtils.isEmpty(skuInfoBean.getCommodityFormat().getGoodsFormatName())){
                holder.allorder_shop_color.setText(skuInfoBean.getCommodityFormat().getGoodsFormatName());
            }else {
                Toast.makeText(mContext,"",Toast.LENGTH_SHORT).show();
            }
        }
        if (!TextUtils.isEmpty(list.get(position).getRefundPrice()+"")){
            holder.allorder_shop_price.setText("￥"+list.get(position).getRefundPrice());//订单金额
        }else {
            Toast.makeText(mContext,"",Toast.LENGTH_SHORT).show();
        }

        if (!TextUtils.isEmpty(skuInfoBean.getCommoditySizeJson().getSizeNumberName())){
            holder.allorder_shop_size.setText(skuInfoBean.getCommoditySizeJson().getSizeNumberName());//尺码
        }else {
            Toast.makeText(mContext,"",Toast.LENGTH_SHORT).show();
        }
        if (!TextUtils.isEmpty(list.get(position).getRetreatOrderStatus())){
            setStatus(setIntager(list.get(position).getRetreatOrderStatus()),holder.sale_after_status);
        }else {
            Toast.makeText(mContext,"",Toast.LENGTH_SHORT).show();
        }

        holder.check_Info_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                /*Intent intent = new Intent(mContext, LogisticsDetailsActivity.class);
                mContext.startActivity(intent);*/
            }
        });

        holder.itemView.setTag(position);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    //String转换int
    private int setIntager(String status){
        if (status != null){
            mStatus = Integer.valueOf(status);
        }
        return mStatus;
    }

    //售后状态判断
    private void setStatus(int x,TextView view){
        if (x == 11){
            view.setText("仅退款 审核中");
        }else if (x == 13){
            view.setText("仅退款 待退款");
        }else if (x == 14){
            view.setText("仅退款 待退款");
        }else if (x == 15){
            view.setText("仅退款 退款成功");
        }else if (x == 16){
            view.setText("仅退款 退款关闭");
        }else if (x == 21){
            view.setText("退款退货 审核中");
        }else if (x == 22){
            view.setText("退款退货 退货中");
        }else if (x == 23){
            view.setText("仅退款 待退款");
        }else if (x == 24){
            view.setText("仅退款 待退款");
        }else if (x == 25){
            view.setText("仅退款 待退款");
        }else if (x == 26){
            view.setText("退款退货 退款关闭");
        }
    }

    @Override
    public void onClick(View view) {
        if (mOnClickListener != null) {
            //注意这里使用getTag方法获取position
            mOnClickListener.onClick(view, (int) view.getTag());
        }
    }


    public class SaleAfterHolder extends RecyclerView.ViewHolder{

        private final ImageView allorder_shop_iv;
        private final TextView allorder_shop_name;
        private final TextView allorder_shop_count;
        private final TextView goods_count;
        private final TextView allorder_shop_size;
        private final TextView allorder_shop_color;
        private final TextView allorder_shop_price;
        private final TextView sale_after_status;
        private final Button check_Info_btn;

        public SaleAfterHolder(View itemView) {
            super(itemView);
            allorder_shop_iv = (ImageView)itemView.findViewById(R.id.allorder_shop_iv);
            allorder_shop_name = (TextView) itemView.findViewById(R.id.allorder_shop_name);
            allorder_shop_count = (TextView) itemView.findViewById(R.id.allorder_shop_count);
            goods_count = (TextView) itemView.findViewById(R.id.goods_count);
            allorder_shop_size = (TextView) itemView.findViewById(R.id.allorder_shop_size);
            //商品颜色
            allorder_shop_color = (TextView) itemView.findViewById(R.id.allorder_shop_color);
            allorder_shop_price = (TextView) itemView.findViewById(R.id.allorder_shop_price);
            sale_after_status = (TextView) itemView.findViewById(R.id.sale_after_status);
            check_Info_btn = (Button) itemView.findViewById(R.id.check_Info_btn);
        }
    }


    public interface OnClickListener {
        void onClick(View view, int position);
    }
    private OnClickListener mOnClickListener = null;
    public void setOnClickListener(OnClickListener listener) {
        this.mOnClickListener = listener;
    }
}

